-
-
Notifications
You must be signed in to change notification settings - Fork 17
Fix casing issues with React attributes #21
Conversation
This aims to resolve the casing differences between hast properties and react attributes. In particular it solves issues with SVG attributes such as stroke-linecap, stroke-linejoin etc. This was discussed here - https://spectrum.chat/unified/rehype/using-an-external-parser-with-rehype-react~5d05b5d8-518a-4902-a110-bc3ec85f5666. I have attempted a generalised solution based on testing for '-' so it is possible there are exceptions that don't fit this rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @jamesopstad! However, there are still some things we need to fix before we can merge. First and foremost, the CI build is currently failing, but we can fix that by doing the following things.
Could you:
- Align with the code style. So
var
instead ofconst
and no arrow functions. - Make the creation of
reactAttribute
conditional and move it into it's own function.
if (ctx.react && info.space) {
var hasDash = info.attribute.includes('-')
props[hasDash ? camelCase(info.attribute) : info.attribute] = value
} else {
props[info.attribute] = value
}
- Create a test to make sure it's fixed.
It's going to be a few days before I can look at this properly again. I've just discovered that the react-dom repository contains a file here mapping all possible DOM attributes to their React equivalents. I think using this and simply looking up the attribute name may be a more robust approach that avoids any possible exceptions. What do you think? |
@jamesopstad Taking a couple of days is no problem, we’re here to stay, and we appreciate any help you can give. And we also like helping newcomers. React indeed has a nice mapping, which may be better to implement compared to simple camelcasing. I’m a bit worried about including the whole file though, especially for people not using React (this library is used for others frameworks as well, such as Vue). What are your thoughts, given this? |
We can:
Inluding the (un-minified) file will increase package size from 22,5kb --> 35,5kb (+57%). Which feels like a lot for a package that isn't necessarily meant for React. On the other hand, we might run into lots of exceptions in custom camel casing. I'd say we create the test first, as it needs to be in there either way, and try our own hand at it. If there turn out to be too many exceptions we can always reconsider. |
It involves a bit of code and thought, but I think a simple camelcase utility with an overwrites object makes sense! |
I've just written a test that inputs all the attributes in possibleStandardNames and compares the hast and react output names. Any mismatches are added to the
The reactNames file contains the data from possibleStandardNames but grouped into separate objects for HTML and SVG schemas. This test returns 44 exceptions. I will put the output here for reference. As you can see, this is a bit more complex than I thought as there isn't a predictable pattern here. Some of them aren't standard attributes though so the next question is, do you want to support all of these?
|
Thank you so much for investigating this! ✨ React specific
Some of these are React-specific: if you pass Deprecated and new attributes
These have all been removed, sometimes without a trace, or are very new (not in the spec) Different names
If the hast property is not lowercase, that means we in hast use a different name than react does. I think these are the ones this issue is really about. I think it may make sense to generate a file of exceptions, from the react names, in |
Including the following functions reduces the exception list to 18.
This would simply mean passing
Have a look and see what you think but I'm not sure that any of these are necessary. If any of them are then we can have a very small list of additional exceptions and test if the output of Happy to make the changes to the code if you wish to go ahead with this approach but I'll need some help writing a test. |
@jamesopstad Thanks for all your research! I was able to make a list of exceptions and published them as With this released, you should be able to change your PR to a) import that JSON file, and b) do something like this: if (ctx.react && info.space) {
props[hastToReact[info.property] || info.property] = value
} else {
props[info.attribute] = value
} ...that should work! Oh and could you create a test that shows that the current state is broken? Thanks! |
Thanks @jamesopstad, appreciate it! |
This aims to resolve the casing differences between hast properties and react attributes. In particular it solves issues with SVG attributes such as stroke-linecap, stroke-linejoin etc. This was discussed here - https://spectrum.chat/unified/rehype/using-an-external-parser-with-rehype-react~5d05b5d8-518a-4902-a110-bc3ec85f5666. I have attempted a generalised solution based on testing for '-' so it is possible there are exceptions that don't fit this rule.